import sys


def process(n,ai,qnum):
    c = []
    c.append(qnum)
    for j in range(n):
        if c[0] in ai[j]:
            x = j
            for y in range((n-1),-1,-1):
                if c[0] in ai[j][y]:
                    return(x+1),(y+1)
    return -1,0


try:
    t = int(input())
    if t < 1 or t > 10:
        sys.exit(0)
    for i in range(t):
        ai = []
        n = int(input())
        for i in range(n):
            temp = list(input().split())
            ai.append((temp))
        q = int(input())
        for i in range(q):
            temp2 = input()
            x, y = process(n, ai, temp2)
            if x == -1:
                print(-1)
            else:
                print(x, y)

except ValueError:
    pass
except EOFError:
    pass
except MemoryError:
    pass
